<?php
namespace app\index\logic;
use think\Db;

class SaleHistory
{
    /*
     * 获取用户消费记录
     * */
    public function getList()
    {
        $params['page']     = isset($params['Pages']) ? $params['Pages'] : 1;
        $params['limit']    = isset($params['Size']) ? $params['Size'] : 20;

        $field = "novel_id AS nid,chapter_no,used_point AS book_point,used_coin AS virtual_currency,read_time AS create_time";

        $where[] = ['uid','=',request()->userInfo['id']];
        $where[] = ['pay_read','=',1];
        $orderBy = [ 'id' => 'DESC'];

        $date = date('Ym');
        $SaleModel = \think\facade\App::model('SaleHistory', 'model');
        $list = $SaleModel->getList($where,$field,$params['page'],$params['limit'],$orderBy,'',$date);

        if($list)
        {
            $nid_gh = array_unique(array_column($list,'nid'));
            $novels = Db::name('novel')->where('id','in',$nid_gh)->column('id,origin_title');
            foreach ($list as $k=>$v)
            {

                $list[$k]['chapter_title'] = '第'.$v['chapter_no'].'章';
                $list[$k]['bookname'] = isset($novels[$v['nid']]) ? $novels[$v['nid']] : '';
            }
        }

        return $list;
    }

    /*
     * 检测消费凭证是否存在
     * */
    public function checkConsume($uid,$novel_id,$chapter_no)
    {
        $where[] = ['uid','=',$uid];
        $where[] = ['novel_id','=',$novel_id];
        $where[] = ['chapter_no','=',$chapter_no];

        $record = Db::table('quick_user_consume_record')->where($where)->field('id')->findOrEmpty();

        if($record)
        {
            return true;
        }else{
            return false;
        }

    }

}